<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Discusses
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class DiscussModel extends Model
{
	protected $belongsTo    = 'Member';
	protected $refererClass;
	public    $defaultOrder = 'created ASC';
	public    $paranoia     = false;
	
	// callbacks
	
	function beforeValidate()
	{
		if (empty($this->validate))
		{
			$this->validate = array(
				'body' => array(
					Valid::NotEmpty  => _('You must write a comment.'),
					'checkBody'      => _("BBcode markup is disallowed."),
					'checkBodyHTML'  => _("HTML links are disallowed.")
				),
				'user_name'  => array('checkUserName' => _("You ain't an URL, are you?")),
				'user_email' => array('checkEmail'    => _('This email is invalid.')),
			);
			
			if ($this->paranoia)
				$this->validate['body']['checkBodyLinks'] = _("There are too many links and not enough words! Are you a spammer?");
		}
		return true;
	}
	
	// a username can't be an URL
	function checkUserName($field, $value)
	{
		return preg_match('#^(http|ftp)[s]?://.+#', $value);
	}
	
	// an email can't be an URL
	function checkEmail($field, $value)
	{
		if (!empty($value))
			return !preg_match(Valid::Email, $value);
		return false;
	}
	
	// body can't contain [url=...]...[/url]
	function checkBody($field, $value)
	{
		return preg_match('#\[url[=]?[^\]]*\][^\[]+\[/url\]#i', $value);
	}
	
	// body can't contain <a href="...">
	function checkBodyHTML($field, $value)
	{
		return preg_match('#\<a[^>]*href=["\']?.+["\']?\>#i', $value);
	}
	
	// If there are more than 20% of links in body, this is spam.
	function checkBodyLinks($field, $value)
	{
		$value = preg_split('/\s/', $value, -1, PREG_SPLIT_NO_EMPTY);
		$words = count($value);
		$links = count(preg_grep('#((http|ftp|mailto)[s]?://[^\s]+?)#i', $value));
		return (100 / $words * $links > 20);
	}
	
	function beforeSave()
	{
		if (!$this->id)
		{
			$this->data[$this->name]['user_ip']   = $_SERVER["REMOTE_ADDR"];
			$this->data[$this->name]['user_host'] = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
		}
		if (empty($this->data[$this->name][$this->{$this->refererClass}->foreignKey]) and !empty($this->{$this->refererClass}->id))
			$this->data[$this->name][$this->{$this->refererClass}->foreignKey] = $this->{$this->refererClass}->id;
		return parent::beforeSave();
	}
	
	function afterSave()
	{
		return $this->updateReferer();
	}
	
	function beforeDelete()
	{
		if (!isset($this->deletionIsRecursive) or $this->deletionIsRecursive === false)
		{
			$ref = $this->{$this->refererClass};
			if (empty($ref->id))
				$ref->id = $this->field($ref->foreignKey, null, -1);
		}
		return true;
	}
	
	function afterDelete()
	{
		if (!isset($this->deletionIsRecursive) or $this->deletionIsRecursive === false)
			return $this->updateReferer();
		return true;
	}
	
	// internals
	
	protected function updateReferer()
	{
		if ($this->controller->action != 'edit')
		{
			$ref = $this->{$this->refererClass};
			
			if (empty($ref->id))
				$ref->id = $this->field($ref->foreignKey, null, -1);
			$this->conditions(array($ref->foreignKey => $ref->id, 'tb' => 0));
			$nb_com = $this->findCount(-1, false);
			
#			$this->conditions(array('tb' => 1));
#			$nb_tb  = $this->findCount(-1);
			
			return $ref->saveFields(array(
				'discuss_nb_com' => $nb_com,
#				'discuss_nb_tb'  => $nb_tb
			));
		}
		return true;
	}
}
?>